﻿/*================================================================*
 *
        Copyright (C) 2021 All rights reserved www.hsby.link
      	�ļ����ƣ�socket.cc
      	�� �� �ߣ���ɭ����
      	�������ڣ�2021/12/19
 *================================================================*/

#include "log.h"
#include "bytearray.h"

void test_dev() {

    // ���Write
    hsby::ByteArray ba1(2);
    std::string str = "1234567890abcdefghij";
    ba1.Write(str.c_str(), str.size());
    ba1.SetWritePos(10);
    ba1.Write(str.c_str(), str.size());

    // �����ļ�д��Ͷ���
    ba1.WriteToFile("bytearray.out");
    hsby::ByteArray ba2(3);
    ba2.ReadFromFile("bytearray.out");

    // ���Read
    char buf[4096];
    hsby::MemSetZero(buf);
    ba2.SetReadPos(5);
    ba2.Read(buf, 20);
    //HSBY_LOG_SYSTEM_DEBUG << buf;

    // ���Data
    //HSBY_LOG_SYSTEM_DEBUG << ba2.DataToTextString();
    //HSBY_LOG_SYSTEM_DEBUG << ba2.DataToHexString();

    // ���GetReadIovecs
    std::vector<iovec> iovs;
    ba2.GetReadIovecs(iovs, str.size(), 3);
    for (const auto &iov: iovs) {
        HSBY_LOG_SYSTEM_DEBUG << (char*)iov.iov_base;
    }
}

void test_int(size_t base_size) {

#define XX(Type, ReadFunc, WriteFunc)      \
    std::vector<Type> Type##_vec;        \
    Type##_vec.reserve(2000);                                          \
    hsby::ByteArray Type##_ba(base_size);                          \
    for (size_t i = 0; i < 2000; ++i) {               \
        Type num = i % 100;                    \
        Type##_ba.WriteFunc(num);                          \
        Type##_vec.push_back(num);                     \
    }                                               \
    for (size_t i = 0; i < Type##_vec.size(); ++i) {   \
        Type num = Type##_ba.ReadFunc(); \
        if (num != Type##_vec[i]){         \
             HSBY_ASSERT(false);                              \
        }\
    }
//        //HSBY_LOG_SYSTEM_DEBUG << num << ", " << Type##_vec[i];

    XX(uint8_t, ReadFuint8, WriteFuint8);
    XX(uint16_t, ReadFuint16, WriteFuint16);
    XX(uint32_t, ReadFuint32, WriteFuint32);
    XX(uint64_t, ReadFuint64, WriteFuint64);
    XX(int8_t, ReadFint8, WriteFint8);
    XX(int16_t, ReadFint16, WriteFint16);
    XX(int32_t, ReadFint32, WriteFint32);
    XX(int64_t, ReadFint64, WriteFint64);
//
//    XX(uint32_t, ReadUint32, WriteUint32);
//    XX(uint64_t, ReadUint64, WriteUint64);
//    XX(int32_t, ReadInt32, WriteInt32);
//    XX(int64_t, ReadInt64, WriteInt64);
}


int main() {
//    test_int(1);
//    test_int(10);
//    test_int(20);
//    test_int(31);
//    test_int(113);
//    test_int(1251);
    test_int(1);
    test_int(1);
    test_int(1);
    test_int(1);
    test_int(1);
    test_int(1023);
    test_int(1123);
    test_int(1123);
    test_int(1123);
    test_int(1123);
    test_int(1023);
    return 0;
}